package com.shared.job;

import com.shared.dao.PGoodsMapper;
import com.shared.dao.POrderItemMapper;
import com.shared.dao.POrdersMapper;
import com.shared.pojo.PGoods;
import com.shared.pojo.POrderItem;
import com.shared.pojo.POrders;
import com.shared.server.POrdersServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

@Component
public class POrderJob {

    @Autowired
    private POrdersMapper pOrdersMapper;
    @Autowired
    private POrdersServer pOrdersServer;
    @Autowired
    private PGoodsMapper pGoodsMapper;
    @Autowired
    private POrderItemMapper pOrderItemMapper;

    //@Scheduled(cron = "0/10 * * * * ?")
    public void initOrder() {
        Long res;
        List<POrders> list = pOrdersMapper.findOrder();
        for (POrders pOrder : list) {
            Date date = pOrder.getFoundtime();
            long ts = date.getTime() + 30 * 60 * 1000;
            res = new Date().getTime();
            if (ts <= res) {
                pOrdersMapper.updateOrder(pOrder.getId(), 5);
                pOrdersServer.updateCountById(pOrder.getId(), 1);
                List<POrderItem> itemList = pOrderItemMapper.findByOrdersn(pOrder.getOrdernsn());
                for (POrderItem pOrderItem : itemList) {
                    pGoodsMapper.updateCartNumberById(pOrderItem.getGoodsid(), pOrderItem.getGoodsnum());
                }
            }
        }
    }
}
